import { createRouter } from '@gowiny/uni-router'
import PAGE_DATA from '@/pages.json'
import { useCommonStore, useUserStore } from '@/stores'
// https://ext.dcloud.net.cn/plugin?id=7724

const router = createRouter({
    pageData:PAGE_DATA
})

// 非认证路径
const nonattestPathes = [
    '/pages/message_login/index',
    '/pages/password_login/index',
    '/pages/register/index'
]

router.beforeEach(async (to,from)=>{
    console.log('beforeEach from = ', from)
    console.log('beforeEach to = ', to)
    // 公共存储
    const commonStore = useCommonStore()

    // 用户存储
    const userStore = useUserStore()

    // 设置当前路径
    commonStore.currentRouteUrl = to.path || ''

    // 刷新token
    await userStore.refreshToken(commonStore.currentRouteUrl)

    // 判断是否需要登录
    if ((!nonattestPathes.includes(commonStore.currentRouteUrl) && userStore.profile.token == '') || (!nonattestPathes.includes(commonStore.currentRouteUrl) && userStore.profile.user == null)) {
        console.log('判断是否需要登录');
        
        return uni.redirectTo({url: '/pages/password_login/index'})
    }

})



// router.afterEach((to,from)=>{
//     console.log('afterEach from = ', from)
//     console.log('afterEach to = ', to)
// })
export default router